package com.yc.home.cloudother.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yc.bean.Result;
import com.yc.entity.Type;
import com.yc.home.cloudother.mapper.TypeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lzk
 * @since 2024-09-22
 */
@RestController
@RequestMapping("/type")
public class TypeController {

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    TypeMapper typeMapper;

    //获取大类别
    @RequestMapping("getBigType")
    Result getBigType(){
        if(redisTemplate.hasKey("BigType")){
            Object object = redisTemplate.opsForValue().get("BigType");
            return new Result(1, "大类别", object);
        }else{
            //redis没有缓存
            LambdaQueryWrapper<Type> lqw = new LambdaQueryWrapper<>();
            lqw.isNull(Type::getTid);
            lqw.eq(Type::getStatus, 1);
            List<Type> types = typeMapper.selectList(lqw);
            // 保存到redis
            redisTemplate.opsForValue().set("BigType", types
                    , 60 * 60 * 24, TimeUnit.SECONDS);
            return new Result(1, "大类别", types);
        }
    }

    //获取小类别
    @RequestMapping("getSmType")
    Result getSmType(String id){
        LambdaQueryWrapper<Type> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Type::getStatus, 1);
        lqw.like(Type::getTid, "%" + ","+id+"," + "%"); //中间id
        lqw.or().like(Type::getTid, id);                    //单个id
        lqw.or().like(Type::getTid, "%"+","+id);        //最后一个id
        lqw.or().like(Type::getTid, id+","+"%");            //第一个id
        List<Type> types = typeMapper.selectList(lqw);
        return new Result(1, "小类别查询", types);
    }

    //获取类别
    @RequestMapping("getType")
    Result getType(){
        LambdaQueryWrapper<Type> lqw = new LambdaQueryWrapper<>();
        lqw.isNotNull(Type::getTid);
        lqw.eq(Type::getStatus, 1);
        List<Type> types = typeMapper.selectList(lqw);
        return new Result(1, "类别", types);
    }

    @RequestMapping("gettypeL")
    Page gettypeL(@RequestParam(defaultValue = "1") int currentPage4, @RequestParam(defaultValue = "10") int pagesize, String tname, String status){
     Page<Type> page = new Page<>(currentPage4, pagesize);
     LambdaQueryWrapper<Type> lqw = new LambdaQueryWrapper<>();
     if(!"".equals(tname)){
         lqw.eq(Type::getTname, tname);
     }
     if(!"-1".equals(status)){
         lqw.eq(Type::getStatus, status);
     }
     Page<Type> typePage = typeMapper.selectPage(page, lqw);
     return typePage;
    }

    @RequestMapping("down")
    Result down(String id){
        Type type = typeMapper.selectById(id);
        type.setStatus(0);
        typeMapper.updateById(type);
        return new Result(1, "成功", null);
    }

    @RequestMapping("up")
    Result up(String id){
        Type type = typeMapper.selectById(id);
        type.setStatus(1);
        typeMapper.updateById(type);
        return new Result(1, "成功", null);
    }

    @RequestMapping("add")
    Result add(String name, String pic, String tid){
        if("".equals(tid)){
            tid = null;
        }
        typeMapper.in(name, pic, tid);
        return new Result(1, "成功", null);
    }

    @RequestMapping("updatetype")
    Result updatetype(String id, String tname, String pic, String tid){
        if("".equals(tid)){
            tid = null;
        }
        typeMapper.up(id, tname, pic, tid);
        return new Result(1, "成功", null);
    }

}

